package com.kvest.view_pager_test.FragmentDialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.kvest.view_pager_test.R;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 18.03.13
 * Time: 0:05
 * To change this template use File | Settings | File Templates.
 */
public class DialogFromLayout extends DialogFragment implements View.OnClickListener {
    private static final String SELECTED_ITEM_EXTRA = "selected_item";
    private String selectedItem;

    public DialogFromLayout() {
        this("?");
    }

    public DialogFromLayout(String selectedItem) {
        super();
        this.selectedItem = selectedItem;
    }

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        if (savedState != null && savedState.containsKey(SELECTED_ITEM_EXTRA)) {
            selectedItem = savedState.getString(SELECTED_ITEM_EXTRA);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().setTitle("DialogFromLayout");
        View view = inflater.inflate(R.layout.dialog_layout, null);
        ((TextView)view.findViewById(R.id.textView1)).setText("Selected: " + selectedItem);
        view.findViewById(R.id.btnMaybe).setOnClickListener(this);
        view.findViewById(R.id.btnYes).setOnClickListener(this);
        view.findViewById(R.id.btnNo).setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }

    @Override
    public void onSaveInstanceState(Bundle savedState) {
        super.onSaveInstanceState(savedState);
        savedState.putString(SELECTED_ITEM_EXTRA, selectedItem);
    }
}
